<?php

/**
 * JingYao-backend
 *
 * @link     https://gitee.com/wang-zhihui-release/jingyao-backend
 * @apiDocument https://gitee.com/wang-zhihui-release/jingyao-backend/wikis/
 */

namespace App\Http\Controllers;

use App\Exceptions\ApiException;
use App\Format\HouserScoreFormat;
use App\Http\Requests\AdminSetHouserScoreRequest;
use App\Http\Requests\CheckRequest;
use App\Http\Requests\CreateHouserScoreRequest;
use App\Services\HouserScoreService;
use Illuminate\Http\Request;

class HouserScoreController extends Controller
{
    protected $houserScoreService;

    public function __construct(HouserScoreService $houserScoreService)
    {
        $this->houserScoreService = $houserScoreService;
    }

    public function setScore(CreateHouserScoreRequest $createHouserScoreRequest)
    {
        $format = new HouserScoreFormat($createHouserScoreRequest->all());
        $this->houserScoreService->setScore($format);
        return renderSuccessJson();
    }

    public function getMySetScore(Request $request)
    {
        $userId = $request->input('user_id');
        $houseId = $request->input('house_id');
        $res = $this->houserScoreService->getMySetScore($userId, $houseId);
        return renderSuccessJson($res);
    }

    public function getMySetScoreList(Request $request)
    {
        $userId = $request->input('user_id');
        $page = $request->input('page', 1);
        $pageSize = $request->input('page_size', 10);
        $search = [];
        if ($request->input('houser_name')) {
            $search['houser_name'] = $request->input('houser_name');
        }
        $res = $this->houserScoreService->getMySetScoreList($userId, $page, $pageSize, $search);
        return renderSuccessJson($res);
    }

    public function getScoreList(Request $request)
    {
        $page = $request->input('page', 1);
        $pageSize = $request->input('page_size', 10);
        $search = [];
        if ($request->input('start_date')) {
            $search['start_date'] = $request->input('start_date');
        }
        if ($request->input('end_date')) {
            $search['end_date'] = $request->input('end_date');
        }
        if ($request->input('check_status')) {
            $search['check_status'] = $request->input('check_status');
        }
        if ($request->input('houser_name')) {
            $search['houser_name'] = $request->input('houser_name');
        }
        $res = $this->houserScoreService->getHouseScoreList($page, $pageSize, $search);
        return renderSuccessJson($res);
    }

    public function checkHouserScore(CheckRequest $checkRequest)
    {
        $id = $checkRequest->input('id');
        $checkStatus = $checkRequest->input('check_status');
        $checkMessage = $checkRequest->input('check_message') ?? '';
        $this->houserScoreService->checkHouseScore($id, $checkStatus, $checkMessage);
        return renderSuccessJson();
    }

    public function adminSetHouserScore(AdminSetHouserScoreRequest $adminSetHouserScoreRequest)
    {
        $houserId = $adminSetHouserScoreRequest->input('houser_id');
        $score = $adminSetHouserScoreRequest->input('score');
        $this->houserScoreService->adminSetHouserScore($houserId, $score);
        return renderSuccessJson();
    }

    public function adminUnSetHouserScore(Request $request)
    {
        $houserId = $request->input('houser_id');
        if (empty($houserId)) {
            throw new ApiException('缺少参数', 1);
        }
        $this->houserScoreService->adminUnSetHouserScore($houserId);
        return renderSuccessJson();
    }
}
